<template>
  <div class="Housing">
    <h2>房源管理</h2>
    <el-tabs type="border-card">
      <el-tab-pane label="用户管理">
        <div class="top_input">
          <!-- 请选者省份 -->
          <el-select v-model="value" placeholder="请选择省份">
            <el-option
              v-for="item in tableData"
              :key="item.value"
              :label="item.name"
              :value="item.value"
            >
              <span style="float: left">{{ item.name }}</span>
            </el-option>
          </el-select>
          <!-- 请选者城市 -->
          <el-select v-model="valu" placeholder="请选择城市">
            <el-option
              v-for="item in tableData"
              :key="item.valu"
              :label="item.city"
              :value="item.valu"
            >
              <span style="float: left">{{ item.city }}</span>
            </el-option>
          </el-select>
          <!-- 请选者区县 -->
          <el-select v-model="val" placeholder="请选择城市">
            <el-option
              v-for="item in tableData"
              :key="item.val"
              :label="item.city"
              :value="item.val"
            >
              <span style="float: left">{{ item.city }}</span>
            </el-option>
          </el-select>
          <!-- 搜索 -->
          <div class="input_button">
            <el-input v-model="input" placeholder="关键字"></el-input>
            <el-button type="primary" @click="kwywor">搜索</el-button>
          </div>
        </div>
        <!-- 新增 -->
        <el-button type="primary" @click="addLi()">新增</el-button>

        <el-dialog title="添加" :visible.sync="dialogFormVisible">
          <el-form :model="form">
            <el-form-item label="楼盘名称" :label-width="formLabelWidth">
              <el-input v-model="form.name" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="所诉区域" :label-width="formLabelWidth">
              <el-input v-model="form.city" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="价格" :label-width="formLabelWidth">
              <el-input v-model="form.price" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="时间" :label-width="formLabelWidth">
              <el-input v-model="form.time" autocomplete="off"></el-input>
            </el-form-item>
          </el-form>
          <div slot="footer" class="dialog-footer">
            <el-button @click="dialogFromVisWidth = false">取 消</el-button>
            <el-button type="primary" @click="add()">确 定</el-button>
          </div>
        </el-dialog>
        <!-- 表格 -->
        <el-table
          :data="
            tableData.slice(
              (currentPage - 1) * pagesize,
              currentPage * pagesize
            )
          "
          border
          width="100%"
        >
          <el-table-column prop="id" label="序号" width="50"> </el-table-column>
          <el-table-column prop="city" label="楼盘名称" >
          </el-table-column>
          <el-table-column prop="name" label="所属区域">
          </el-table-column>
          <el-table-column prop="price" label="价格">
          </el-table-column>
          <el-table-column prop="time" label="更新时间">
          </el-table-column>
          <el-table-column prop="price" label="排序" >
          </el-table-column>
          <el-table-column prop="status" label="状态">
            <el-switch
              v-model="vaue"
              active-color="#13ce66"
              inactive-color="#ff4949"
            >
            </el-switch>
          </el-table-column>
          <el-table-column prop="address" label="操作" width="500">
            <template slot-scope="scope">
              <!-- 户型 -->
              <el-button type="text" size="small">户型 </el-button>
              <!-- 相册 -->
              <el-button type="text" size="small">相册 </el-button>
              <!-- 动态 -->
              <el-button type="text" size="small">动态 </el-button>
              <!-- 沙盘 -->
              <el-button type="text" size="small">沙盘 </el-button>
              <!-- 编辑 -->
              <el-button
                @click.native.prevent="edit(scope.row, tableData)"
                type="text"
                size="small"
              >
                编辑
              </el-button>
              <el-dialog title="编辑" :visible.sync="dialogForm">
                <el-form :model="form">
                  <el-form-item label="姓名" :label-width="formLabelWidth">
                    <el-input v-model="form.name" autocomplete="off"></el-input>
                  </el-form-item>
                  <el-form-item label="手机号" :label-width="formLabelWidth">
                    <el-input
                      v-model="form.price"
                      autocomplete="off"
                    ></el-input>
                  </el-form-item>
                  <el-form-item label="公司" :label-width="formLabelWidth">
                    <el-input v-model="form.city" autocomplete="off"></el-input>
                  </el-form-item>
                  <el-form-item label="租房" :label-width="formLabelWidth">
                    <el-input v-model="form.time" autocomplete="off"></el-input>
                  </el-form-item>
                </el-form>
                <div slot="footer" class="dialog-footer">
                  <el-button @click="dialogForm = false">取 消</el-button>
                  <el-button type="primary" @click="editLi()">确 定</el-button>
                </div>
              </el-dialog>
              <!-- 删除 -->
              <el-button @click="header(scope.row.id)" type="text" size="small"
                >删除</el-button
              >
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页 -->
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          layout="prev, pager, next"
          :page-size="6"
          :total="tableData.length"
        >
        </el-pagination>
      </el-tab-pane>
      <el-tab-pane label="配置管理">配置管理</el-tab-pane>
    </el-tabs>
  </div>
</template>

<script>
import axios from "axios";
export default {
  data() {
    return {
      tableData: [],
      value: "",
      valu: "",
      val: "",
      input: "",
      vaue: true,

      total: 2, //总页
      currentPage: 1, //初始页
      pagesize: 6, //每页数据

      dialogFormVisible: false,
      form: {
        name: "",
        price: "",
        city: "",
        time: "",
      },
      dialogForm: false,
      form: {
        name: "",
        price: "",
        city: "",
        time: "",
      },
      formLabelWidth: "120px",
    };
  },
  created() {
    //获取数据
    axios.get("http://127.0.0.1:7001/getList").then((res) => {
      this.tableData = res.data.data;
      console.log("res", res);
      this.total = this.tableData.length;
    });
  },
  methods: {
    //删除
    header(id) {
      axios.delete(`http://127.0.0.1:7001/delete/${id}`).then((res) => {
        this.$router.go(0);
        console.log("id.;.;.;", id);
      });
    },
    //搜索
    kwywor() {
      axios
        .get(`http://127.0.0.1:7001/getList/?keyword=${this.input}`)
        .then((res) => {
          this.tableData = res.data.data;
        });
    },
    //添加
    addLi() {
      this.dialogFormVisible = true;
    },
    add() {
      axios
        .post("http://127.0.0.1:7001/addList", { ...this.form })
        .then((res) => {
          this.dialogFormVisible = false;
          this.$router.go("0");
        });
    },
    //编辑
    edit(row) {
      this.dialogForm = true;
      Object.assign(this.form, { ...row });
    },
    editLi() {
      axios
        .put("http://127.0.0.1:7001/editList", { ...this.form })
        .then((res) => {
          this.dialogForm = false;
          this.$router.go(0);
        });
    },
    //分页
    handleSizeChange: function(size) {
      this.pagesize = size;
    },
    handleCurrentChange: function(currentPage) {
      this.currentPage = currentPage;
    },
    //状态开关
    changeSwitch(row) {
      console.log(row);
    },
  },
};
</script>

<style>
.el-tabs__item {
  border: 1px solid #ccc;
}
.el-tabs__nav-scroll {
  background: #fff;
}
.el-button {
  margin-bottom: 20px;
}

.el-pagination {
  text-align: right;
  padding: 10px 0;
}

.top_input {
  width: 100%;
  display: flex;
}
.el-table__row {
  height: 50px;
}
</style>
